package com.fled.itsmbigscreen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fled.itsmbigscreen.dto.*;
import com.fled.itsmbigscreen.dto.FindHighBugClient.FindHighBugClientDto;
import com.fled.itsmbigscreen.dto.clientInfoDto.ClientListDto;
import com.fled.itsmbigscreen.dto.clientInfoDto.InfoList;
import com.fled.itsmbigscreen.dto.clientListDto.ClientList;
import com.fled.itsmbigscreen.dto.clientListDto.ClientListData;
import com.fled.itsmbigscreen.dto.groupDto.HuorongBaseList;
import com.fled.itsmbigscreen.enums.HuorongUrlEnums;
import com.fled.itsmbigscreen.mapper.HuorongSysLogMapper;
import com.fled.itsmbigscreen.pojo.HuorongSysLog;
import com.fled.itsmbigscreen.pojo.HuorongSysLogExample;
import com.fled.itsmbigscreen.service.HuorongService;
import com.fled.itsmbigscreen.utils.HuorongUtils;
import com.fled.itsmbigscreen.utils.PictureUtils;
import com.fled.itsmbigscreen.utils.TimeTool;
import com.fled.itsmbigscreen.vo.DefVo;
import com.fled.itsmbigscreen.vo.GroupAddVo;
import com.fled.itsmbigscreen.vo.getDefCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HuorongServiceImpl implements HuorongService {

    @Autowired
    private HuorongSysLogMapper huorongSysLogMapper;


    @Override
    public HuorongBaseList GroupList() throws NoSuchAlgorithmException, InvalidKeyException {
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.grouList, null);
        HuorongBaseList huorongBaseList = JSONObject.parseObject(authorization, HuorongBaseList.class);
        return huorongBaseList;
    }

    /**
     * 获取所有设备及信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ClientListDto ClientListWithInfo(Integer pageNum, Integer pageSize) throws NoSuchAlgorithmException, InvalidKeyException {
        Boolean flag = Boolean.FALSE;
        HuorongBasePage huorongBasePage = new HuorongBasePage();
        if (pageNum == null || pageSize == null) {
            flag = true;
            huorongBasePage.setLimit(200);
        } else {
            huorongBasePage.setLimit(pageSize);
            huorongBasePage.setOffset(pageNum);
        }

        List<InfoList> clientIds = new ArrayList<>();
        List<String> ids = new ArrayList<>();
        //先查询设备列表
        clientIds = this.getClientIds(huorongBasePage, clientIds, ids);
        if (clientIds.size() <= 0) {
            return null;
        }
        ClientListDto clientInfoData = new ClientListDto();
        clientInfoData.setList(clientIds);
        return clientInfoData;
    }

    /**
     * 、
     * 获取终端列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ClientListData ClientList(Integer pageNum, Integer pageSize) throws NoSuchAlgorithmException, InvalidKeyException {
        HuorongBasePage huorongBasePage = new HuorongBasePage();
        if (pageNum == null || pageSize == null) {
            huorongBasePage.setLimit(200);
        } else {
            huorongBasePage.setLimit(pageSize);
            huorongBasePage.setOffset(pageNum);
        }
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.clientList, JSONObject.toJSONString(huorongBasePage));
        ClientListData clientListData = JSONObject.parseObject(authorization, ClientListData.class);
        return clientListData;
    }

    /**
     * 根据设备ids获取详细信息
     *
     * @param clientIds
     * @return
     */
    @Override
    public ClientListDto clientIds(String clientIds) throws NoSuchAlgorithmException, InvalidKeyException {
        List<String> ids = new ArrayList<>();
        for (String s : clientIds.split(",")) {
            ids.add(s);
        }
        HuorongBaseClientsDto huorongBaseClientsDto = new HuorongBaseClientsDto();
        huorongBaseClientsDto.setClients(ids);
        String clientAuthorization = HuorongUtils.createAuthorization(HuorongUrlEnums.clientInfoList, JSONObject.toJSONString(huorongBaseClientsDto));
        ClientListDto clientInfoData = JSONObject.parseObject(clientAuthorization, ClientListDto.class);
        return clientInfoData;
    }

    /**
     * 查询高危漏洞设备
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public FindHighBugClientDto findHighBugClient(Integer pageNum, Integer pageSize) throws NoSuchAlgorithmException, InvalidKeyException {
        HuorongBasePage huorongBasePage = new HuorongBasePage();
        if (pageNum == null || pageSize == null) {
            huorongBasePage.setLimit(10);
        } else {
            huorongBasePage.setLimit(pageSize);
            huorongBasePage.setOffset(pageNum);
        }
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.FindHighBugList , JSONObject.toJSONString(huorongBasePage));
        FindHighBugClientDto findHighBugClientDto = JSONObject.parseObject(authorization, FindHighBugClientDto.class);
        return findHighBugClientDto;
    }

    /**
     * 创建分组
     *
     * @param groupAddDto
     * @return
     */
    @Override
    public GroupAddVo GroupAdd(GroupAddDto groupAddDto) throws NoSuchAlgorithmException, InvalidKeyException {
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.GroupAdd , JSONObject.toJSONString(groupAddDto));
        GroupAddVo groupAddVo = JSONObject.parseObject(authorization, GroupAddVo.class);
        return groupAddVo;
    }

    /**
     * 删除分组
     *
     * @param groupDelDto
     */
    @Override
    public void GroupDel(GroupDelDto groupDelDto) throws NoSuchAlgorithmException, InvalidKeyException {
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.GroupDel , JSONObject.toJSONString(groupDelDto));
        System.out.println(authorization);
//        GroupAddVo groupAddVo = JSONObject.parseObject(authorization, GroupAddVo.class);
    }

    /**
     * 修改分组名称
     *
     * @param groupEditNameDto
     */
    @Override
    public void GroupEditName(GroupEditNameDto groupEditNameDto) throws NoSuchAlgorithmException, InvalidKeyException {
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.GroupRename , JSONObject.toJSONString(groupEditNameDto));
        System.out.println(authorization);
    }

    /**
     * 获取所有在线设备数量
     *
     * @return
     */
    @Override
    public Integer onlineNum() throws NoSuchAlgorithmException, InvalidKeyException {
        List<ClientList> totalList = new ArrayList<>();
        ClientListData clientListData = this.ClientList(0,200);
        totalList.addAll(clientListData.getList());
        while (totalList.size()<clientListData.getList().size()){
            totalList.addAll(this.ClientList(totalList.size(),200).getList());
        }
        return null;
    }

    /**
     * 获取今日防御数量
     *
     * @return
     */
    @Override
    public int getDefNum() {
        HuorongSysLogExample example = new HuorongSysLogExample();
        example.createCriteria().andAddTimeBetween(TimeTool.getDayBegin(),TimeTool.getDayEnd());
        List<HuorongSysLog> huorongSysLogs = huorongSysLogMapper.selectByExample(example);
        return huorongSysLogs.size();
    }

    /**
     * 获取防护中心历史
     *
     * @return
     */
    @Override
    public List<DefVo> getDefCenter() {
        Date beginDayOfLastWeek = TimeTool.getBeginDayOfLastWeek();
        Date dayEnd = TimeTool.getDayEnd();
        List<DefVo> defVo = huorongSysLogMapper.getDefVo(TimeTool.dateFormat(beginDayOfLastWeek),TimeTool.dateFormat(dayEnd));
        return defVo;
    }


    public List<InfoList> getClientIds(HuorongBasePage huorongBasePage, List<InfoList> clientInfoList, List<String> clientIds) throws NoSuchAlgorithmException, InvalidKeyException {
        String authorization = HuorongUtils.createAuthorization(HuorongUrlEnums.clientList, JSONObject.toJSONString(huorongBasePage));
        ClientListData clientListData = JSONObject.parseObject(authorization, ClientListData.class);
        if (clientListData != null && clientListData.getList().size() > 0) {
            //判断是否是查询所有
            List<ClientList> list = clientListData.getList();
//            if (flag) {
            for (ClientList clientList : list) {
                clientIds.add(clientList.getClient_id());
            }
            //根据获取的设备ids查询设备详细信息
            HuorongBaseClientsDto huorongBaseClientsDto = new HuorongBaseClientsDto();
            huorongBaseClientsDto.setClients(clientIds);
            String clientAuthorization = HuorongUtils.createAuthorization(HuorongUrlEnums.clientInfoList, JSONObject.toJSONString(huorongBaseClientsDto));
            ClientListDto clientInfoData = JSONObject.parseObject(clientAuthorization, ClientListDto.class);
            clientInfoList.addAll(clientInfoData.getList());
            if (list.size() < huorongBasePage.getLimit()) {
                huorongBasePage.setOffset(huorongBasePage.getLimit()+1);
                huorongBasePage.setLimit(200);
                this.getClientIds(huorongBasePage, clientInfoList, clientIds);
            } else {
                return clientInfoList;
            }
//            }
        }
        return clientInfoList;

    }

    public static void main(String[] args) throws Exception {
        String s = PictureUtils.downloadImage("http://cdn.fledchina.com/Fi_jx9nRqZytMXcZZGNO3RBjG9Sm");
        System.out.printf(s);
    }


}
